using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

using Presentation.Contracts;
using Presentation;

namespace ProjektZespolowy.Forum
	{
	public partial class Default : System.Web.UI.Page, IMyForumsView
		{
		#region Members

		/// <summary>
		/// Instance of presenter.
		/// </summary>
		private MyForumsPresenter presenter;
		private int myOwnerForumsStartIndex = 0;
		private int myForumsStartIndex = 0;

		#endregion Members

		#region Override Methods

		/// <summary>
		/// Raises the <see cref="E:System.Web.UI.Control.Init"></see> event.
		/// </summary>
		/// <param name="e">An <see cref="T:System.EventArgs"></see> object that contains the event data.</param>
		protected override void OnInit(EventArgs e)
			{
			base.OnInit(e);
			presenter = new MyForumsPresenter(this);
			if (this.LoggedUserID > 0)
				{
				presenter.PageMyOwnerForums();
				presenter.PageMyForums();
				}
			}

		#endregion Override Methods

		#region Private Methods
		protected void Page_Load(object sender, EventArgs e)
			{
			if (!IsPostBack)
				{
				if (this.LoggedUserID > 0)
					{
					presenter.Initialize();
					}
				else
					{
					notLoggedLabel1.Visible = true;
					notLoggedLabel2.Visible = true;
					}
				}
			}
		#endregion Private Methods

		#region IMyForumsView Members

		public void ShowMyOwnerForums(DataTable forumTable)
			{
			DataList2.DataSource = forumTable;
			DataList2.DataBind();
			
			}

		public void ShowMyForums(DataTable forumTable)
			{
			DataList1.DataSource = forumTable;
			DataList1.DataBind();
			}

		public void ShowSearchedForums(DataTable forumTable)
			{
			throw new Exception("The method or operation is not implemented.");
			}

		public string SearchedString
			{
			get
				{
				return TextBox1.Text;
				}
			set
				{
				TextBox1.Text = value;
				}
			}

		public string SearchingColumn
			{
			get
				{
				throw new Exception("The method or operation is not implemented.");
				}
			set
				{
				throw new Exception("The method or operation is not implemented.");
				}
			}

		#endregion

		protected void Button1_Click(object sender, EventArgs e)
			{
			if (this.SearchedString.Length > 0)
				{
				Response.Redirect("SearchForums.aspx?SearchText=" + SearchedString);
				}
			}


		#region IMyForumsView Members


		public int MyOwnerForumsStartIndex
			{
			get
				{
				return this.myOwnerForumsStartIndex;
				}
			set
				{
				this.myOwnerForumsStartIndex = value;
				}
			/*
			get {
			if (Request.QueryString["MyOwnerForumsStartIndex"] == null)
				return 0;
			else
				return Int32.Parse(Request.QueryString["MyOwnerForumsStartIndex"].ToString());
				}
			 */ 
			}

		public int MyForumsStartIndex
			{
			get
				{
				return this.myForumsStartIndex;
				}
			set
				{
				this.myForumsStartIndex = value;
				}
			}

		public int LoggedUserID
			{
			get
				{
				if (Session[Globals.ContentNamesGlobals.SessionWithUserId] != null)
					return Int32.Parse(Session[Globals.ContentNamesGlobals.SessionWithUserId].ToString());
				else
					return -1;
				}
			}


		#endregion

		public void PageMyOwnerForums(int itemsPerPage,int howMany)
			{
			List<LinkButton> tmp = GeneratePagingLinks("Default.aspx", "MyOwnerForumsStartIndex", "ItemsPerPage", itemsPerPage, howMany);
			int counter = 0;
			foreach (LinkButton hl in tmp)
				{
				this.Panel1.Controls.Add(hl);
				hl.ID = "PageMyOwnerForumsButton" + counter.ToString();
				hl.Click += new EventHandler(LinkButton1_Click);
				Label tmpLabel = new Label();
				tmpLabel.Text = " | ";
				this.Panel1.Controls.Add(tmpLabel);
				counter++;
				}
			}

		public void PageMyForums(int itemsPerPage,int howMany)
			{
			List<LinkButton> tmp = GeneratePagingLinks("Default.aspx", "MyOwnerForumsStartIndex", "ItemsPerPage", itemsPerPage, howMany);
			int counter = 0;
			foreach (LinkButton hl in tmp)
				{
				this.Panel3.Controls.Add(hl);
				hl.ID = "PageMyForumsButton" + counter.ToString();
				hl.Click += new EventHandler(LinkButton2_Click);
				Label tmpLabel = new Label();
				tmpLabel.Text = " | ";
				this.Panel3.Controls.Add(tmpLabel);
				counter++;
				}
			}

		private List<LinkButton> GeneratePagingLinks(string webSite, string queryStartParameter, string queryItemsPerPageParameter, int itemsPerPage, int howMany)
			{
			int counter = 1;
			List<LinkButton> returnList = new List<LinkButton>();
			for (int i = 0; i < howMany; i+=itemsPerPage)
				{
				LinkButton tmp = new LinkButton();
				int st = 0 + (counter - 1) * itemsPerPage;
				string linkstring = webSite + "?" + queryStartParameter + "=" + st.ToString() + "&" + queryItemsPerPageParameter +
					"=" + itemsPerPage.ToString();
				//tmp.PostBackUrl = linkstring;
				tmp.Text = counter.ToString();
				returnList.Add(tmp);
				counter++;
				}
			return returnList;	
			}

		protected void LinkButton1_Click(object sender, EventArgs e)
			{
			this.MyOwnerForumsStartIndex = (Int32.Parse(((LinkButton)(sender)).Text)-1) * 4;
			presenter.Initialize();
			}
		
		protected void LinkButton2_Click(object sender, EventArgs e)
			{
			this.MyForumsStartIndex = (Int32.Parse(((LinkButton)(sender)).Text) - 1) * 4;
			presenter.Initialize();
			}

		}
	}
